﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Twilight.Controls
{
    public class BubbleTweet : Control
    {
        private TweetBlock tweetBlock;
        private TweetInfo tweetInfo;
        private Rectangle bubble;
        private Rectangle shadow;

        public BubbleTweet()
        {
            DefaultStyleKey = typeof(BubbleTweet);
        }

        public bool ShowInfo { get; set; }

        public override void OnApplyTemplate()
        {
            tweetBlock = GetTemplateChild("tweetBlock") as TweetBlock;
            tweetInfo = GetTemplateChild("tweetInfo") as TweetInfo;
            bubble = GetTemplateChild("bubble") as Rectangle;
            shadow = GetTemplateChild("shadow") as Rectangle;

            tweetBlock.SizeChanged += new SizeChangedEventHandler(tweetBlock_SizeChanged);

            if (ShowInfo == false) tweetInfo.Opacity = 0;

            base.OnApplyTemplate();
        }

        void tweetBlock_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            var height = tweetBlock.DesiredSize.Height;

            bubble.Height = height + 7;
            shadow.SetValue(Canvas.TopProperty, height + 17);
            tweetInfo.SetValue(Canvas.TopProperty, height + 17);
        }
    }
}
